package org.lemon.cla.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.lemon.cla.pojo.ClubActivity;
import org.lemon.cla.vo.ClubActivityVo; // Import ClubActivityVo
import org.lemon.cla.dto.request.ClubActivitySearchRequest;

@Mapper
public interface ClubActivityMapper extends BaseMapper<ClubActivity> {

    // Common select fields for joining user table
    String JOIN_USER_SELECT_FIELDS =
            "u.username AS organizerUsername, " +
                    "u.avatar_url AS organizerAvatarUrl, " +
                    "u.real_name AS organizerRealName, " +
                    "u.phone AS organizerPhone, " +
                    "u.email AS organizerEmail, " +
                    "u.role AS organizerRole, " +
                    "u.is_deleted AS organizerIsDeleted "; // Include is_deleted status

    /**
     * Get ClubActivity details by ID, including organizer information.
     * @param activityId ClubActivity ID
     * @return ClubActivity view object
     */
    @Select("SELECT ca.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM club_activity ca " +
            "LEFT JOIN user u ON ca.organizer_id = u.user_id " +
            "WHERE ca.activity_id = #{activityId}")
    ClubActivityVo selectActivityVoById(@Param("activityId") Long activityId);

    /**
     * Paginated query for all ClubActivities, including organizer information.
     * @param page Pagination object
     * @return Paginated list of ClubActivity view objects
     */
    @Select("SELECT ca.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM club_activity ca " +
            "LEFT JOIN user u ON ca.organizer_id = u.user_id " +
            "ORDER BY ca.start_time DESC")
    IPage<ClubActivityVo> selectActivityVoPage(IPage<ClubActivityVo> page);

    /**
     * Fuzzy search for ClubActivities based on multiple conditions, including organizer information.
     * @param page Pagination object
     * @param req Search request DTO
     * @return Paginated list of ClubActivity view objects
     */
    @Select("<script>" +
            "SELECT ca.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM club_activity ca " +
            "LEFT JOIN user u ON ca.organizer_id = u.user_id " +
            "<where>" +
            "  <if test='req.clubId != null'>" +
            "    AND ca.club_id = #{req.clubId}" +
            "  </if>" +
            "  <if test='req.activityName != null and req.activityName != \"\"'>" +
            "    AND ca.activity_name LIKE CONCAT('%', #{req.activityName}, '%')" +
            "  </if>" +
            "  <if test='req.content != null and req.content != \"\"'>" +
            "    AND ca.content LIKE CONCAT('%', #{req.content}, '%')" +
            "  </if>" +
            "  <if test='req.location != null and req.location != \"\"'>" +
            "    AND ca.location LIKE CONCAT('%', #{req.location}, '%')" +
            "  </if>" +
            "  <if test='req.signupStartAfter != null'>" +
            "    AND ca.signup_start &gt;= #{req.signupStartAfter}" +
            "  </if>" +
            "  <if test='req.signupStartBefore != null'>" +
            "    AND ca.signup_start &lt;= #{req.signupStartBefore}" +
            "  </if>" +
            "  <if test='req.signupEndAfter != null'>" +
            "    AND ca.signup_end &gt;= #{req.signupEndAfter}" +
            "  </if>" +
            "  <if test='req.signupEndBefore != null'>" +
            "    AND ca.signup_end &lt;= #{req.signupEndBefore}" +
            "  </if>" +
            "  <if test='req.startTimeAfter != null'>" +
            "    AND ca.start_time &gt;= #{req.startTimeAfter}" +
            "  </if>" +
            "  <if test='req.startTimeBefore != null'>" +
            "    AND ca.start_time &lt;= #{req.startTimeBefore}" +
            "  </if>" +
            "  <if test='req.endTimeAfter != null'>" +
            "    AND ca.end_time &gt;= #{req.endTimeAfter}" +
            "  </if>" +
            "  <if test='req.endTimeBefore != null'>" +
            "    AND ca.end_time &lt;= #{req.endTimeBefore}" +
            "  </if>" +
            "  <if test='req.organizerId != null'>" +
            "    AND ca.organizer_id = #{req.organizerId}" +
            "  </if>" +
            "  <if test='req.participantCountMin != null'>" +
            "    AND ca.participant_count &gt;= #{req.participantCountMin}" +
            "  </if>" +
            "  <if test='req.participantCountMax != null'>" +
            "    AND ca.participant_count &lt;= #{req.participantCountMax}" +
            "  </if>" +
            "</where>" +
            "ORDER BY ca.start_time DESC" +
            "</script>")
    IPage<ClubActivityVo> selectActivityVoByMultiConditions(
            IPage<ClubActivityVo> page,
            @Param("req") ClubActivitySearchRequest request
    );
}